/**
 * Created by Administrator on 2017/3/19 0019.
 */
import {message} from "antd";
export default {
    removedarrayitem(aar, Int){
        let tempindex = -1;
        aar.map((temp, index)=> {
            if (Int == temp) {
                tempindex = index;
            }
        }); 
        if (tempindex != -1) {
            aar.splice(tempindex, 1);
        }
    },
    /*日期格式化*/
    dateFormat(time){
        if(time == undefined || time == null ||time == "")return;
        let zero=(n)=>{
            return n = n>0&&n<10 ? "0"+n : n;
        };
        let y = new Date(time).getFullYear();
        let m = new Date(time).getMonth() + 1;
        let d = new Date(time).getDate();
        return zero(y)+"-"+zero(m)+"-"+zero(d)
    },
    dateFormat_hms(time){
        let zero=(n)=>{
            return n = n>0&&n<10 ? "0"+n : n;
        };
        let y = new Date(time).getFullYear();
        let m = new Date(time).getMonth() + 1;
        let d = new Date(time).getDate();
        var h = new Date(time).getHours();
        var min = new Date(time).getMinutes();
        if (min == '0') {
          min = '00'
        }
        return zero(y)+"-"+zero(m)+"-"+zero(d)+" "+zero(h)+":"+zero(min)
    },
    timeStamp(stringTime) {
      // 获取某个时间格式的时间戳(此种写法不严谨：2015-1-1 和 2015-01-01 获取到的时间戳不一样,后台返回到的时间戳没有除以1000)
      // var timestamp2 = Date.parse(new Date(stringTime));
      // timestamp2 = timestamp2 / 1000;
      //2014-07-10 10:21:12的时间戳为：1404958872
        /*更改为：*/
        if(!stringTime ||stringTime == ""){
            return ""
        }
        let date = new Date(Date.parse(stringTime.replace(/-/g, "/")));
        date = date.getTime();
        return date
    },
    dayRate(time){
        if(time == undefined || time == null ||time == "")return;
        let zero=(n)=>{
            return n = n>0&&n<10 ? "0"+n : n;
        };
        let y = new Date(time).getFullYear();
        let m = new Date(time).getMonth() + 1;
        let d = new Date(time).getDate();
        return zero(m)+"/"+zero(d)
    },
    /*金额千分制*/
    doubleFormat(obj){
        if(!obj){
            return obj;
        }else{
            return parseFloat(obj).toLocaleString();
        }
    },
    /*数字处理-保留n位小数*/
    decimalPoint(number,n){
        if(number == undefined || NaN)return;
        if(parseFloat(number) == NaN)return;
        let m = n?n:2;
        return parseFloat(number).toFixed(m)
    },

    numtoChinese(num){
        let mnum = parseFloat(num);
        let strOutput="",strTemp="",strInTemp='';
        let unitArray = new Array("圆万亿","仟佰拾","零壹贰叁肆伍陆柒捌玖");
        let mnumArray = mnum.toString().split('.');
        let integralnum = mnumArray[0];
        let integrallen = integralnum.length;
        let decimalnum = (mnum.toString().indexOf('.')>=0) ? mnumArray[1].substr(0, 2) : '0';
        let decimallen = decimalnum.length;
        let ints = parseInt(integrallen/4);
        let inty = integrallen%4;
        if (ints>3 || (ints==3 && inty>0)) return "超出范围";
        if (inty>0)
        {
            ints++;
            integralnum = "0000".substr(inty)+integralnum;
            integrallen = integralnum.length;
        }
        var i = 0;
        while (i<integrallen)
        {
            let strOutTemp = "";
            strTemp = integralnum.substr(i, 4);
            i += 4;
            for (let j=0; j<4; j++)
            {
                strInTemp = parseInt(strTemp.substr(j, 1));
                strOutTemp += unitArray[2].substr(strInTemp, 1);
                if (strInTemp>0 && j<3) strOutTemp += unitArray[1].substr(j, 1);
            }
            strOutTemp = strOutTemp.replace(/零+$/, "");
            ints--;
            if (strOutTemp!="") strOutTemp += unitArray[0].substr(ints, 1);
            if (strTemp.substr(3,1)=='0') strOutTemp += "零";
            strOutput += strOutTemp;
        }
        strOutput = strOutput.replace(/零+/g, "零").replace(/^零/, "").replace(/零$/, "");
        if (strOutput=="圆") strOutput = "";
        if (decimallen==2)
        {
            strOutput += (decimalnum.charAt(0)!='0') ? unitArray[2].substr(parseInt(decimalnum.charAt(0)), 1)+"角" : "零";
            if (strOutput=="零") strOutput = "";
            strOutput += unitArray[2].substr(parseInt(decimalnum.charAt(1)), 1)+"分";
        }
        else
        {
            if (decimalnum!='0') strOutput += unitArray[2].substr(parseInt(decimalnum), 1)+"角";
            if (strOutput!="") strOutput += "整";
        }
        if (strOutput=="") strOutput = "金额为零";
        return strOutput;
    },

    signedContractStr(url) {
      url = url.split('?')[1]
      let url1 = url.split('&')
      let arr = {}
      url1.forEach(function(v, i, ele){
        return arr[v.split('=')[0]]  = v.split('=')[1]
      })
      return arr.attname
    },
    /*提示-定时器-》传this指针: this.messageFlag=true;*/
    messageHint(this_,type,hintText){
        /*this.messageFlag默认true*/
        let _this = this_;
        hintText = hintText?hintText:"";
        if (_this.messageFlag) {
            _this.messageFlag = false;
            switch (type){
                case "info":
                    message.info(hintText);
                    break;
                case "warning":
                    message.warning(hintText);
                    break;
                case "error":
                    message.error(hintText);
                    break;
                case "success":
                    message.success(hintText);
                    break;
                default:
                    message.info(hintText);
                    break;
            }
            clearTimeout(_this.messageTime);
            _this.messageTime = setTimeout(() => {
                _this.messageFlag = true;
            }, 1800);
            return
        }
    },
    //判断元素有无类名
    hasClass(curEle,strClass){//判断：判断元素curEle是否有(一个)类名strClass
        let reg=new RegExp('\\b'+strClass+'\\b');//实例创建正则可以变量字符串拼接
        //var reg=new RegExp('(^| +)'+strClass+'( +|$)');
        return reg.test(curEle.className); //正则校验元素类名 返回true或false
    },
    //添加类名
    addClass(curEle,strClass){//设置：向元素curEle添加(一组)类名strClass
        let aryClass=strClass.replace(/(^ +)|( +$)/g,'').split(/\s+/g);//类名转数组
        //循环的目的是为了逐个校验
        for(let i=0; i<aryClass.length; i++){//循环类名的数组中每一项
            if(!this.hasClass(curEle,aryClass[i])){//元素原本的类名中不包含此项
                curEle.className+=' '+aryClass[i];//添加此类名
            }
        }
    },
    //移除类名
    removeClass(curEle,strClass){//设置：删除元素curEle的(一组)类名strClass；
        let aryClass=strClass.replace(/(^ +)|( +$)/g,'').split(/\s+/g);//转数组--去除首尾空格+中间空格切分为数组
        for(let i=0; i<aryClass.length; i++){//循环类名的数组
            //var reg=new RegExp('(^| +)'+aryClass[i]+'( +|$)');
            let reg=new RegExp('\\b'+aryClass[i]+'\\b');//正则--匹配某个类名
            if(reg.test(curEle.className)){//用正则校验元素的类名
                curEle.className=curEle.className.replace(reg,' ').replace(/(^ +)|( +$)/g,'').replace(/\s+/g,' ');
                //用空格途欢匹配到的类名，在去除首尾空格和中间的多个空格
            }
        }
    }
}
